package com.cdu.ai_intelligent_teaching_and_training_system.controller;

import com.cdu.ai_intelligent_teaching_and_training_system.dto.ApiResponse;
import com.cdu.ai_intelligent_teaching_and_training_system.entity.Assignment;
import com.cdu.ai_intelligent_teaching_and_training_system.entity.AssignmentQuestion;
import com.cdu.ai_intelligent_teaching_and_training_system.service.AssignmentService;
import com.cdu.ai_intelligent_teaching_and_training_system.service.AssignmentQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/assignments")
@CrossOrigin(origins = "*")
public class AssignmentController {
    
    @Autowired
    private AssignmentService assignmentService;
    
    @Autowired
    private AssignmentQuestionService assignmentQuestionService;
    
    @GetMapping
    public ResponseEntity<ApiResponse<List<Assignment>>> getAllAssignments() {
        try {
            List<Assignment> assignments = assignmentService.findAll();
            return ResponseEntity.ok(ApiResponse.success(assignments));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取作业列表失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Assignment>> getAssignmentById(@PathVariable Integer id) {
        try {
            Assignment assignment = assignmentService.findById(id).orElse(null);
            if (assignment == null) {
                return ResponseEntity.ok(ApiResponse.error("作业不存在"));
            }
            return ResponseEntity.ok(ApiResponse.success(assignment));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取作业详情失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/course/{courseId}")
    public ResponseEntity<ApiResponse<List<Assignment>>> getAssignmentsByCourse(@PathVariable Integer courseId) {
        try {
            List<Assignment> assignments = assignmentService.findByCourseId(courseId);
            return ResponseEntity.ok(ApiResponse.success(assignments));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取课程作业失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/overdue")
    public ResponseEntity<ApiResponse<List<Assignment>>> getOverdueAssignments() {
        try {
            List<Assignment> assignments = assignmentService.findOverdueAssignments();
            return ResponseEntity.ok(ApiResponse.success(assignments));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取逾期作业失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<List<Assignment>>> searchAssignments(@RequestParam String title) {
        try {
            List<Assignment> assignments = assignmentService.findByTitleContaining(title);
            return ResponseEntity.ok(ApiResponse.success(assignments));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("搜索作业失败：" + e.getMessage()));
        }
    }
    
    @PostMapping
    public ResponseEntity<ApiResponse<Assignment>> createAssignment(@Valid @RequestBody Assignment assignment) {
        try {
            Assignment savedAssignment = assignmentService.save(assignment);
            return ResponseEntity.ok(ApiResponse.success("作业创建成功", savedAssignment));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("创建作业失败：" + e.getMessage()));
        }
    }
    
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Assignment>> updateAssignment(@PathVariable Integer id, @Valid @RequestBody Assignment assignment) {
        try {
            Assignment existingAssignment = assignmentService.findById(id).orElse(null);
            if (existingAssignment == null) {
                return ResponseEntity.ok(ApiResponse.error("作业不存在"));
            }
            
            assignment.setAssignmentId(id);
            Assignment updatedAssignment = assignmentService.save(assignment);
            return ResponseEntity.ok(ApiResponse.success("作业更新成功", updatedAssignment));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("更新作业失败：" + e.getMessage()));
        }
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<String>> deleteAssignment(@PathVariable Integer id) {
        try {
            Assignment existingAssignment = assignmentService.findById(id).orElse(null);
            if (existingAssignment == null) {
                return ResponseEntity.ok(ApiResponse.error("作业不存在"));
            }
            
            assignmentService.deleteById(id);
            return ResponseEntity.ok(ApiResponse.success("作业删除成功", null));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("删除作业失败：" + e.getMessage()));
        }
    }
    
    @GetMapping("/{id}/questions")
    public ResponseEntity<ApiResponse<List<AssignmentQuestion>>> getAssignmentQuestions(@PathVariable Integer id) {
        try {
            List<AssignmentQuestion> questions = assignmentQuestionService.findByAssignmentId(id);
            return ResponseEntity.ok(ApiResponse.success(questions));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("获取作业题目失败：" + e.getMessage()));
        }
    }
    
    @PostMapping("/{id}/questions")
    public ResponseEntity<ApiResponse<AssignmentQuestion>> addQuestion(@PathVariable Integer id, @Valid @RequestBody AssignmentQuestion question) {
        try {
            question.setAssignmentId(id);
            AssignmentQuestion savedQuestion = assignmentQuestionService.save(question);
            return ResponseEntity.ok(ApiResponse.success("题目添加成功", savedQuestion));
        } catch (Exception e) {
            return ResponseEntity.ok(ApiResponse.error("添加题目失败：" + e.getMessage()));
        }
    }
} 